classdef ET_COGENT < ET_GEN
    % This subclass uses cogent as a graphical motor.
    
    properties
        
        % Buffers

        blank      = 1
        ready      = 2
        fixation   = 3
        pause_str  = 4
        
        % keyboard

        ESC        = 52
        
    end

    methods
        
        function prepareOccluder(self,sizes,positions)

                self.occluder = OCCLUDER_SIMPLE(self,sizes,positions);
            
        end
        
        function prepareJump(self)
            self.jump = JUMP(self);
            self.jump.m(:) = 0;
        end
        function prepareTrialTypes(self)
            self.FpC = ones(self.num_blocks,self.num_trials);
            self.trialtypes = self.FpC;
            % Randomization
            tmp = kron([1,1],1:numel(self.FpClearn));
            for b = 1:self.num_blocks
                self.trialtypes(b,:) = tmp(randperm(self.num_trials));
                self.FpC(b,:) = self.FpClearn(self.trialtypes(b,:));
            end
        end
        function prepareFixation(self)
            self.randomFixation = round(rand(self.num_blocks,self.num_trials));
            self.randomFixation(self.randomFixation == 0) = -1;
            self.randomFixation = self.randomFixation * self.fix.x;
        end 
        
        function start_GM(self)
            % Initializes the graphical motor, i.e., cogent or PTB.

            if self.Eyelink
                Eyelink('Initialize');
            end

            config_display(self.scn.mon,self.scn.res)
            config_keyboard
            start_cogent
            
            % Prepare strings
            clearpict(self.blank)
            clearpict(self.ready)
            clearpict(self.fixation);
            preparestring('Press a key when ready...',self.ready)
            preparestring('Pause',self.pause_str)
            drawpict(self.ready);

            % Fixation cross at small step from start of stimulus
            drawpict(self.ready);
            waitkeydown(inf);
            drawpict(self.blank); % possibly?
        end

        function runScreen(self)

            % Start dialog
            self.startDialog()
            % Preamble
            self.start_GM()
            function [goout,kn]= escapeTrial()
                goout = 0;
                readkeys
                [k,kt,kn]=getkeydown(self.ESC);
                if kn
                    b = self.num_blocks;
                    t = self.num_trials;
                    goout = 1;
                end
                [k,kt,ks]=getkeydown(71);
                if ks;
                    drawpict(self.ready);
                    waitkeydown(inf);
                end
            end
            
            indexes = reshape(1:self.num_blocks * self.num_trials,...
                self.num_blocks,self.num_trials)';
            for trial = indexes(:)'
                [b,t]= ind2sub([self.num_blocks,self.num_trials],trial);
                preparestring('+', self.fixation, self.randomFixation(trial)*self.scn.w/2,...
                    self.fix.y*self.scn.h/2)
                drawpict(self.fixation);
                tic; while toc < self.fix.time/2; end
                
                % Open new eyelink file
                %----------------------------------------------------------
                if self.Eyelink
                    Eyelink('Openfile', ['s' num2str(self.Epar.Sbj) 'e' ...
                        num2str(self.Epar.Exp) 'b' num2str(b) ...
                        't' num2str(t) '.edf']);
                    Eyelink('StartRecording');
                    self.sendparameters(trial);
                end
                
                tic; while toc < self.fix.time/2; end
                
                if self.Eyelink;
                    Eyelink ('Message', 'START');
                end
                
                c  = 1;
                dt = 0;
                
                while dt <= self.cycles
                    dt = dt+(1/self.FpC(trial));
                    
                    if c && abs(dt - self.jump.frame(trial)) <= 2/self.FpC(trial)
                        dt = dt + self.jump.m(trial);
                        c = 0;
                    end
                    
                    cx = self.stimuli(dt,self.FpC(trial)) * self.randomFixation(trial)*self.scn.w/2;
                    
                    % Draw pictures
                    %------------------------------------------------------
                    cgellipse(cx,0,self.stim.size,self.stim.size, self.stim.rgb, 'f');
                    cgellipse(cx,0,self.stim.size/3,self.stim.size/3, [0,0,0], 'f');
                    cgrect(...
                        self.occluder.X(trial)+self.occluder.W(trial)/2, ...
                        self.occluder.y,self.occluder.W(trial), ...
                        self.occluder.h, self.occluder.rgb);
                    cgflip(0,0,0);
                    
                    % Check keyboard events
                    %------------------------------------------------------
                    [goout,kn] = escapeTrial();
                    if goout; break; end
                end

                % Saves file
                %----------------------------------------------------------
                if self.Eyelink
                    Eyelink('Closefile');
                    Eyelink('ReceiveFile');
                end
                if kn; break; end

                % Transition between trials
                %----------------------------------------------------------
                self.trialTransition();

                % Pause
                % ---------------------------------------------------------
                if ~mod(t,self.pauses)
                    t0 = drawpict(self.pause_str);
                    waituntil(t0+1000);
                    stop_cogent
                    if self.Eyelink; Eyelink('Shutdown'); end
                    input('Press enter to continue\n');
                    % Restart
                    %------------------------------------------------------
                    self.start_cogent() 
                end
                
            end
            
            if self.Eyelink; Eyelink('Shutdown'); end
            stop_cogent
            
            self.clearFunction(self.stimuli);
            
        end

        function trialTransition(self)
            drawpict(self.fixation);
            cgflip(0,0,0);
            tic; while toc < self.fix.time; end
            clearpict(self.fixation);
            drawpict(self.blank);
            cgflip(0,0,0);
            while toc < self.fix.time; end
        end
        
    end
end